package com.itheima.controller;

import com.itheima.common.R;

import com.itheima.pojo.dto.orderDto.OrderAddDto;
import com.itheima.pojo.vo.orderVo.OrderByPageVo;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public R add(@RequestBody OrderAddDto order){
        boolean add = orderService.add(order);
        if (add) {
            shoppingCartService.clean();
            return R.success("提交成功");
        }
        return R.error("提交失败");
    }
    @GetMapping("/userPage")
    public R selectByPage(Integer page,Integer pageSize){
        List<OrderByPageVo> list = orderService.selectByPage(page, pageSize);
        Map<String,Object>map=new HashMap<>();
        map.put("records", list);
        map.put("total", (page-1)*pageSize);
        map.put("size", pageSize);
        map.put("current",page-1);
        map.put("hitCount", false);
        map.put("countId", null);
        map.put("maxLimit",null);
        map.put("searchCount", true);
        map.put("pages", page);
        return R.success(map);
    }
}
